package com.jmu.content.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jmu.content.dto.UserLoginDTO;
import com.jmu.content.dto.UserPageDTO;
import com.jmu.content.mapper.UserMapper;
import com.jmu.content.pojo.ChargePile;
import com.jmu.content.pojo.User;
import com.jmu.content.properties.JwtProperties;
import com.jmu.content.properties.WeChatProperties;
import com.jmu.content.result.PageResult;
import com.jmu.content.service.UserService;

import com.jmu.utils.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fems
 * @since 2024-05-06
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    WeChatProperties weChatProperties;

    @Autowired
    JwtProperties jwtProperties;

    public static final String WX_LOGIN = "https://api.weixin.qq.com/sns/jscode2session";

    @Override
    public User wxLogin(UserLoginDTO userLoginDTO) {
        String openid = getOpenid(userLoginDTO.getCode());
        log.info("openid is",openid);
        //对openId判空
        if (openid == null){
            return null;
        }

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getOpenid,openid);
        //判断是否为新用户
        User user = this.getOne(queryWrapper);
        if (user == null){
            user = User.builder()
                    .openid(openid)
                    .build();
            this.save(user);
        }
        log.info("user is",user.toString());
        return user;
    }

    @Override
    public PageResult pageQuery(UserPageDTO dto) {
        Page<ChargePile> page = PageHelper.startPage(dto.getPage(), dto.getPageSize());
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dto.getName()!=null&&dto.getName().length()!=0,User::getName,dto.getName());
        queryWrapper.eq(dto.getStatus()!=null,User::getStatus,dto.getStatus());
        List<User> list = this.list(queryWrapper);
        PageInfo pageInfo = new PageInfo(list);

        return new PageResult(page.getTotal(),pageInfo.getList());
    }

    private String getOpenid(String code){
        //获得openId
        Map<String, String> map = new HashMap<>();
        map.put("appid",weChatProperties.getAppid());
        map.put("secret",weChatProperties.getSecret());
        map.put("js_code",code);
        map.put("grant_type","authorization_code");
        String json = HttpClientUtil.doGet(WX_LOGIN, map);

        JSONObject jsonObject = JSON.parseObject(json);
        String openid = jsonObject.getString("openid");

        return openid;
    }
}
